// pages/register/register.js
const API = require('../../utils/api.js')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: "",
    region: ["请选择","请选择","请选择"]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type
    })
  },

  bindRegionChange:function(e) {
    this.setData({
      region: e.detail.value
    })
  },

  formSubmit:function(e) {
    console.log(e.detail.value)
    //检查信息是否填写完整
    let data = e.detail.value
    let registerData = {}
    if(data.username == ""){
      wx.showToast({
        title: '请输入用户名',
        icon: 'none'
      })
      return
    }else{
      registerData["name"] = data.username
    }
    if(data.password == ""){
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return
    }else{
      registerData["password"] = data.password
    }
    if(this.data.type != "customer"){
      if(data.locationRegion[0] == "请选择" || data.locationRegion[1] == "请选择" || data.locationRegion[2] == "请选择"){
        wx.showToast({
          title: '请选择省市区',
          icon: 'none'
        })
        return
      }
      if(data.locationDetail == ""){
        wx.showToast({
          title: '请输入详细地址',
          icon: 'none'
        })
        return
      }
      if(data.tel == ""){
        wx.showToast({
          title: '请输入联系电话',
          icon: 'none'
        })
        return
      }
      //根据省市区和详细地址信息拼接完整地址，需单独处理直辖市的数据
      if(data.locationRegion[0] == data.locationRegion[1]){
        registerData["location"] = data.locationRegion[1] + data.locationRegion[2] + data.locationDetail
      }else if(data.locationRegion[0] == "重庆市" && data.locationRegion[1] == "县"){
        registerData["location"] = data.locationRegion[0] + data.locationRegion[2] + data.locationDetail
      }else if(data.locationRegion[1].indexOf("直辖") != -1){
        registerData["location"] = data.locationRegion[0] + data.locationRegion[2] + data.locationDetail
      }else{
        registerData["location"] = data.locationRegion[0] + data.locationRegion[1] + data.locationRegion[2] + data.locationDetail
      }
    }else{
      if(data.locationRegion[0] != "请选择" && data.locationRegion[1] != "请选择" && data.locationRegion[2] != "请选择" && data.locationDetail != ""){
        //根据省市区和详细地址信息拼接完整地址，需单独处理直辖市的数据
        if(data.locationRegion[0] == data.locationRegion[1]){
          registerData["location"] = data.locationRegion[1] + data.locationRegion[2] + data.locationDetail
        }else if(data.locationRegion[0] == "重庆市" && data.locationRegion[1] == "县"){
          registerData["location"] = data.locationRegion[0] + data.locationRegion[2] + data.locationDetail
        }else if(data.locationRegion[1].indexOf("直辖") != -1){
          registerData["location"] = data.locationRegion[0] + data.locationRegion[2] + data.locationDetail
        }else{
          registerData["location"] = data.locationRegion[0] + data.locationRegion[1] + data.locationRegion[2] + data.locationDetail
        }
      }else{
        registerData["location"] = ""
      }
    }
    if(data.tel != "" && data.tel.length != 11){
      wx.showToast({
        title: '联系电话格式错误',
        icon: 'none'
      })
      return
    }else{
      registerData["tel"] = data.tel
    }
    console.log(registerData)
    this.register(registerData)
  },

  register:function(data){
    let that = this
    wx.showLoading({
      title: '注册中',
    })
    wx.request({
      url: this.getUpdateApi(this.data.type),
      method: 'POST',
      data: data,
      success(res) {
        wx.hideLoading()
        console.log(res.data)
        let returnData = res.data
        if(returnData.code == "SUCCESS"){
          wx.showToast({
            title: returnData.message,
            icon: 'none'
          })
          app.globalData.usertype = that.data.type
          app.globalData.userinfo = returnData.data
          app.globalData.isLogin = true
          wx.setStorageSync('usertype', that.data.type)
          wx.setStorageSync('userinfo', app.globalData.userinfo)
          wx.setStorageSync('isLogin', true)
          wx.navigateBack({
            delta: 2
          })
        }
        else{
          wx.hideLoading()
          wx.showToast({
            title: returnData.message,
            icon: 'none'
          })
        }
      },
      fail(res){
        wx.hideLoading()
      }
    })
  },

  getUpdateApi: function (type) {
    if (type == "customer") {
      return API.Customer.register
    } else if (type == "seller") {
      return API.Seller.register
    } else if (type == "producer") {
      return API.Producer.register
    } else if (type == "transporter") {
      return API.Transporter.register
    }
  }
})